package com.pr.makfilm.user.utils;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.pr.makfilm.domain.NumberOrderEnum;
import com.pr.makfilm.domain.NumberStyleEnum;
import com.pr.makfilm.domain.Room;
import com.pr.makfilm.domain.RoomPlace;
import com.pr.makfilm.service.util.NumberUtils;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Barbara
 * Date: 15.10.13
 * Time: 00:26
 * To change this template use File | Settings | File Templates.
 */
public class RoomPlaceUtils
{
   public static Table convertToTable(List<RoomPlace> placeList)
   {
      Table<Integer, Integer, RoomPlace> table = HashBasedTable.create();
      for (RoomPlace place : placeList)
      {
         table.put(place.getRow(), place.getColumn(), place);
      }
      return table;
   }

   public static void recalculatePlaceLabels(Room room, List<String> xLabels, List<String> yLabels, Table<Integer, Integer, RoomPlace> placeTable)
   {
      xLabels.clear();
      for (int i = 0; i < room.getColumnCount(); i++)
      {
         appendHeader(xLabels, i, room.getxStyle());
      }

      yLabels.clear();
      for (int i = 0; i < room.getRowCount(); i++)
      {
         appendHeader(yLabels, i, room.getyStyle());
      }

      appendLabels(room, xLabels, yLabels, placeTable);
   }

   public static void appendLabels(Room room, List<String> xLabels, List<String> yLabels, Table<Integer, Integer, RoomPlace> placeTable)
   {
      for (int row = 0; row < room.getRowCount(); row++)
      {
         int i = 0;
         if (room.getxOrder() == NumberOrderEnum.BEGINNING)
         {
            for (int column = 0; column < room.getColumnCount(); column++)
            {
               RoomPlace p = placeTable.get(row, column);
               if (p != null)
               {
                  p.setLabel(xLabels.get(i++));
                  placeTable.put(row, column, p);
               }
            }
         }
         else
         {
            for (int column = room.getColumnCount() - 1; column >= 0; column--)
            {
               RoomPlace p = placeTable.get(row, column);
               if (p != null)
               {
                  p.setLabel(xLabels.get(i++));
                  placeTable.put(row, column, p);
               }
            }
         }
      }
   }

   public static void appendHeader(List<String> list, int i, NumberStyleEnum style)
   {
      if (style == NumberStyleEnum.ARABIC_NUMERIC)
      {
         list.add("" + (i + 1));
      }
      else if (style == NumberStyleEnum.ROMAN_NUMERIC)
      {
         list.add(NumberUtils.toRoman(i + 1));
      }
      else if (style == NumberStyleEnum.ALPHABETIC)
      {
         list.add(NumberUtils.toAlphabetic(i + 1));
      }
   }
}
